package com.mmx.baseservice.user.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mmx.baseservice.admin.pojo.AdInfo;
import com.mmx.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.mmx.baseservice.common.utils.SignDecodeUtil;
import com.mmx.baseservice.common.utils.encrypt.RSAUtil;
import com.mmx.baseservice.common.utils.returnbody.Code;
import com.mmx.baseservice.common.utils.returnbody.ReturnBody;
import com.mmx.baseservice.user.pojo.UsInfo;
import com.mmx.baseservice.user.service.UsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author mmx generator
 * @since 2021-07-23
 */
@Api(tags = "用户端-账号相关接口")
@RestController
@RequestMapping(value = "user/usInfo", produces = "text/plain;charset=utf-8")
public class UsInfoController {

    @Resource
    private UsInfoService usInfoService;


    @ApiOperation("登录验证")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "phone", value = "手机号", required = true, example = "PtivnlNFS8UOjbkUyQ6G6n49+c9xIT8npUwsEPpzBNFm0Pt/BVeZkrY6JSf8Rx71ks359U+0Q8d7Iob/P6G9TvWgq0c0SroSzOpsJy6+E7C4Yl12VoorHNE6EImdmPrEdxiEv0ac7HruYP4Nk+D9BIxpf2T0UgDQduFrOkNRWBo="),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "password", value = "密码", required = true, example = "DLUsX3SZ0otbA5J+XE4VrG9K3f2SiLWnyHvSYrQ5CFAkJoFg0AbHKbZnJJsOmSyWwezQvQRWpnYldXmfTrsCFLCPM1RbSgB97f4Wsg0W94+URsPGRLCzRjQlZaGze/uy/DT996rgAskjGh2/1Vy8NE1NGcoE7XjWW+W+JjIGckc="),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "verifyCode", value = "图片验证码", required = true, example = "1234"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "verifySign", value = "图片验证码密钥", required = true, example = "bOENDfoXAhyXyi8wZ/FsfQxWso6hhErukzPz+s3xQq4=")
    })
    @PostMapping("public/registered")
    @ParameterVerify(notNull = {"account", "password", "verifyCode", "verifySign"})
    public String authority(String account, String password, String verifyCode, String verifySign) throws Exception {
        password = RSAUtil.decryptByPrivateKey(password);
        //验证码验证
        QueryWrapper<UsInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(account != null, AdInfo.ACCOUNT, account);
        JSONObject verifyResult = SignDecodeUtil.codeMatches(verifyCode, verifySign, 1000000);
        if (!verifyResult.getBoolean("isSuccess")) {
            return ReturnBody.error(Code.IMAGE_CODE_ERROR, verifyResult.getString("errorMessage"));
        }
        UsInfo usInfo = usInfoService.getOneForUpdate(new QueryWrapper<UsInfo>().eq(UsInfo.PHONE, account).eq(UsInfo.PASSWORD, password));
        return usInfoService.authority(usInfo);
    }


    
   

}
